from twisted.web2 import stream as stream_mod

@stream_mod.generatorToStream
def ungzipStream(input):
    #very ^2 easy implementation ;->

    #init
    yield input.wait

    _buf = ''
    for buf in input:
        if len(buf) > 0:
            _buf += buf
            yield ''
        yield input.wait

    import gzip, StringIO
    yield gzip.GzipFile(fileobj=StringIO.StringIO(_buf)).read()

@stream_mod.generatorToStream
def inflateStream(input):
    import zlib
    decompress = zlib.decompressobj(zlib.MAX_WBITS)
    _decompress = decompress.decompress
    yield input.wait
    for buf in input:
        if len(buf) > 0:
            yield _decompress(buf)
        yield input.wait
    yield decompress.flush()

__all__ = [
    'ungzipStream',
    'inflateStream',
]
